import cv2

cap = cv2.VideoCapture(5)
cap.set(3, 640)
cap.set(4, 480)
i = 0
while cv2.waitKey(1) != 27:
    success, frame = cap.read()
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) == 32:
        i = i+1
        cv2.imwrite(str(i)+".jpg", frame)
    if cv2.waitKey(5) & 0xFF == 27:
        break

cap.release()
cv2.destroyAllWindows()
